#coding=utf-8
#Boa:Frame:FrameValidation

import wx

from product import *

def create(parent):
    return FrameValidation(parent)

[wxID_FRAMEVALIDATION, wxID_FRAMEVALIDATIONBTNSTART, 
 wxID_FRAMEVALIDATIONGAUGEPROCESS, wxID_FRAMEVALIDATIONSTATICBOX1, 
 wxID_FRAMEVALIDATIONSTATICBOX2, wxID_FRAMEVALIDATIONTCPROCESS, 
 wxID_FRAMEVALIDATIONTCRESULT, 
] = [wx.NewId() for _init_ctrls in range(7)]

class FrameValidation(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAMEVALIDATION,
              name=u'FrameValidation', parent=prnt, pos=wx.Point(228, 77),
              size=wx.Size(1052, 693), style=wx.DEFAULT_FRAME_STYLE,
              title=u'\u6570\u636e\u6821\u5bf9')
        self.SetClientSize(wx.Size(1044, 659))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Center(wx.BOTH)
        self.Bind(wx.EVT_CLOSE, self.OnFrameValidationClose)

        self.tcProcess = wx.TextCtrl(id=wxID_FRAMEVALIDATIONTCPROCESS,
              name=u'tcProcess', parent=self, pos=wx.Point(56, 80),
              size=wx.Size(816, 264), style=wx.TE_MULTILINE, value=u'')

        self.tcResult = wx.TextCtrl(id=wxID_FRAMEVALIDATIONTCRESULT,
              name=u'tcResult', parent=self, pos=wx.Point(56, 400),
              size=wx.Size(816, 232), style=wx.TE_MULTILINE, value=u'')

        self.staticBox1 = wx.StaticBox(id=wxID_FRAMEVALIDATIONSTATICBOX1,
              label=u'\u6821\u9a8c\u8fdb\u7a0b', name='staticBox1', parent=self,
              pos=wx.Point(40, 48), size=wx.Size(840, 304), style=0)

        self.staticBox2 = wx.StaticBox(id=wxID_FRAMEVALIDATIONSTATICBOX2,
              label=u'\u51fa\u73b0\u95ee\u9898', name='staticBox2', parent=self,
              pos=wx.Point(40, 368), size=wx.Size(840, 280), style=0)

        self.btnStart = wx.Button(id=wxID_FRAMEVALIDATIONBTNSTART,
              label=u'\u5f00\u59cb\u6821\u9a8c', name=u'btnStart', parent=self,
              pos=wx.Point(48, 16), size=wx.Size(93, 26), style=0)
        self.btnStart.Bind(wx.EVT_BUTTON, self.OnBtnStartButton,
              id=wxID_FRAMEVALIDATIONBTNSTART)

        self.gaugeProcess = wx.Gauge(id=wxID_FRAMEVALIDATIONGAUGEPROCESS,
              name=u'gaugeProcess', parent=self, pos=wx.Point(160, 16),
              range=100, size=wx.Size(712, 28), style=wx.GA_HORIZONTAL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.tcProcess.SetValue(u'点击按钮，开始校验，这里显示校验信息及修复：\n')
        self.tcResult.SetValue(u'这里显示校验中出现的问题，请复制信息并联系技术支持，修复数据库：\n')

        self.gaugeProcess.SetBezelFace(3)  
        self.gaugeProcess.SetShadowWidth(3)
        
    def OnFrameValidationClose(self, event):
        self.Show(False)

    def OnBtnStartButton(self, event):
        self.gaugeProcess.SetValue(10)
        
        cnt = self.check_saleOder()        
        self.gaugeProcess.SetValue(20)
        
        cnt += self.check_purOder()        
        self.gaugeProcess.SetValue(30)
        
        cnt += self.check_voucher_customer()
        self.gaugeProcess.SetValue(40)
        
        cnt += self.check_voucher_supplier()
        self.gaugeProcess.SetValue(50)
        
        cnt += self.check_saleorder_paid()        
        self.gaugeProcess.SetValue(60)
        
        cnt += self.check_purorder_paid()
        self.gaugeProcess.SetValue(70)
        
        self.gaugeProcess.SetValue(100)
        
        dlg = wx.MessageDialog(self, u'查出问题%s条'%cnt, 
                                u'提示', wx.OK|wx.ICON_ERROR)
        dlg.ShowModal()
        
    def check_saleOder(self):
        self.tcProcess.AppendText(u'\n开始销售单金额的校验...\n')
        result = CDataSaleOrder.valid_total()
        cnt = 0 
        for cust,billid,total,total2,diff in result:
            if abs(diff) > 0.01:
                if cnt == 0:
                    self.tcResult.AppendText(u'\n销售单总金额不同于明细总金额的问题数据：\n')
                cnt += 1
                self.tcResult.AppendText(u'%d.%s,%s,总额%s,明细总额%s,差额%s\n'%(cnt,cust,billid,total,total2,diff))
                            
        self.tcProcess.AppendText(u'结束销售单金额的校验，共校验数据%s条，问题数据%s条\n'%(len(result),cnt))
        return cnt
    
    def check_purOder(self):
        self.tcProcess.AppendText(u'\n开始采购单金额的校验...\n')
        result = CDataPurOrder.valid_total()
        cnt = 0
        for cust,billid,total,total2,diff in result:
            if abs(diff) > 0.01:
                if cnt == 0:
                    self.tcResult.AppendText(u'\n采购单总金额不同于明细总金额的问题数据：\n')
                cnt += 1
                self.tcResult.AppendText(u'%d.%s,%s,总额%s,明细总额%s,差额%s\n'%(cnt,cust,billid,total,total2,diff))
                            
        self.tcProcess.AppendText(u'结束采购单金额的校验，共校验数据%s条，问题数据%s条\n'%(len(result),cnt))
        return cnt
        
    def check_customer(self):
        #self.tcProcess.AppendText(u'\n开始客户数据的校验，暂无校验\n')
        return 0
        
    def check_voucher_supplier(self):       
                
        self.tcProcess.AppendText(u'\n开始采购付款单金额分配的校验...\n')
        result = CDataVoucherSupplier.valid_alloct()
        cnt = 0
        for cust,billid,total,total2,diff in result:
            if diff < 0:
                if cnt == 0:
                    self.tcResult.AppendText(u'\n采购付款单总额小于分配金额的问题数据：\n')
                cnt += 1
                self.tcResult.AppendText(u'%d.%s,%s,总额%s,明细总额%s,差额%s\n'%(cnt,cust,billid,total,total2,diff))
                            
        self.tcProcess.AppendText(u'结束采购付款单金额分配的校验，共校验数据%s条，问题数据%s条\n'%(len(result),cnt))
        return cnt
    
    def check_voucher_customer(self):       
                
        self.tcProcess.AppendText(u'\n开始销售付款单金额分配的校验...\n')
        result = CDataVoucher.valid_alloct()
        cnt = 0
        for cust,billid,total,total2,diff in result:
            if diff < 0:
                if cnt == 0:
                    self.tcResult.AppendText(u'\n销售付款单总额小于分配金额的问题数据：\n')
                cnt += 1
                self.tcResult.AppendText(u'%d.%s,%s,总额%s,明细总额%s,差额%s\n'%(cnt,cust,billid,total,total2,diff))
                            
        self.tcProcess.AppendText(u'结束销售付款单金额分配的校验，共校验数据%s条，问题数据%s条\n'%(len(result),cnt))
        return cnt
    
    def check_saleorder_paid(self):       
                
        self.tcProcess.AppendText(u'\n开始销售单金额分配的校验...\n')
        result = CDataSaleOrder.valid_paid()
        cnt = 0
        for cust,billid,total,discount,total2,diff in result:
            if diff < 0:
                if cnt == 0:
                    self.tcResult.AppendText(u'\n销售单总额小于分配金额的问题数据：\n')
                cnt += 1
                self.tcResult.AppendText(u'%d.%s,%s,总额%s,明细总额%s,差额%s\n'%(cnt,cust,billid,total,total2,diff))
                            
        self.tcProcess.AppendText(u'结束销售单金额分配的校验，共校验数据%s条，问题数据%s条\n'%(len(result),cnt))
        return cnt
    
    def check_purorder_paid(self):       
                
        self.tcProcess.AppendText(u'\n开始采购单金额分配的校验...\n')
        result = CDataPurOrder.valid_paid()
        cnt = 0
        for cust,billid,total,discount,total2,diff in result:
            if diff < 0:
                if cnt == 0:
                    self.tcResult.AppendText(u'\n采购单总额小于分配金额的问题数据：\n')
                cnt += 1
                self.tcResult.AppendText(u'%d.%s,%s,总额%s,明细总额%s,差额%s\n'%(cnt,cust,billid,total,total2,diff))
                            
        self.tcProcess.AppendText(u'结束采购单金额分配的校验，共校验数据%s条，问题数据%s条\n'%(len(result),cnt))
        return cnt